package com.yaoyong.yy_demo.module.spring5.taskscheduler;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class ScheduledTaskService {

    /**
     * @Scheduled 包含多种类型的计划任务:fixedRate,fixDelay,cron等
     */
    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        System.out.println("每隔五秒执行一次: "+LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
    }

    @Scheduled(cron = "0/10 * * * * *")
    public void fixTimeExecution() {
        System.out.println("在指定时间: "+LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))+" 执行");
    }
}
